from django.shortcuts import render
from myTest.models import Category


def top(request):
    content = {}
    return render(request, "top.html", content)


def index(request):
    content = {}
    category_html = []
    # 一级存入数据
    category1 = Category.objects.filter(pid__isnull=True)[:19]
    category1_ids = []
    for cat1 in category1:
        category1_ids.append(cat1.id)
        category_html.append({'id': cat1.id, 'title': cat1.name, "detail": cat1.detail, 'children': []})
    # 二级存入数据
    category2 = Category.objects.filter(pid__id__in=category1_ids)[:20]
    category2_ids = []
    for cat2 in category2:
        category2_ids.append(cat2.id)
        for cat1 in category_html:
            if cat2.pid_id == cat1['id']:
                cat1['children'].append({'id': cat2.id, 'title': cat2.name, "detail": cat2.detail, 'children': []})
    # 三级存入数据
    category3 = Category.objects.filter(pid__id__in=category2_ids)[:100]
    for cat3 in category3:
        for cat1 in category_html:
            for cat2 in cat1['children']:
                if cat3.pid_id == cat2['id']:
                    cat2['children'].append({'id': cat3.id, 'title': cat3.name, "src": cat3.src, "detail": cat3.detail})
    content['category'] = category_html
    return render(request, 'index_view.html', content)
